package com.application.magazine;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.AttributeSet;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.application.magazine.actionitem.ActionItem;
import com.application.magazine.actionitem.QuickAction;
import com.application.magazine.actionitem.QuickAction.OnActionItemClickListener;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.viewpagerindicator.TabPageIndicator;

public class PhotoFragment extends Fragment {

	private ImageView toggle;
	private static QuickAction qa;
	private static final int BOOK_NAME_ID = 1;
	private static final int BOOK_DATE_ID = 2;
	private static final int BOOK_FAV = 3;
	Fragment resultPage = null;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setRetainInstance(true);

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		final Context contextThemeWrapper = new ContextThemeWrapper(
				getActivity(), R.style.Theme_PageIndicatorDefaults);
		// clone the inflater using the ContextThemeWrapper
		LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);

		// inflater the layout
		View view = localInflater.inflate(R.layout.photo_fragment, null);
		toggle = (ImageView) view.findViewById(R.id.btn_photo_toggle);
		ManageQuickAction(0);
		ImageView showMenu = (ImageView) view.findViewById(R.id.btn_magazine_select);
		showMenu.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				qa.show(v);
			}
		});
		toggle.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View paramView) {
				// TODO Auto-generated method stub
				((MainActivity) getActivity()).getSlidingMenu().toggle();
			}
		});
		

		FragmentPagerAdapter adapter = new GoogleMusicAdapter(getActivity().getSupportFragmentManager());

		ViewPager pager = (ViewPager) view.findViewById(R.id.pager);
		pager.setAdapter(adapter);

		TabPageIndicator indicator = (TabPageIndicator) view.findViewById(R.id.indicator);
		indicator.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int paramInt) {
				// TODO Auto-generated method stub
				SlidingMenu sm = ((MainActivity) getActivity()).getSlidingMenu();
				if (paramInt == 0) {
					sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
				} else {
					sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
				}
			}

			@Override
			public void onPageScrolled(int paramInt1, float paramFloat,
					int paramInt2) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onPageScrollStateChanged(int paramInt) {
				// TODO Auto-generated method stub

			}
		});
		indicator.setViewPager(pager);

		return view;
	}

	public void showMsg(String msg) {
	}

	class GoogleMusicAdapter extends FragmentPagerAdapter {

		private final String[] CONTENT = new String[] {
				getActivity().getResources().getString(R.string.pang_book),
				getActivity().getResources().getString(R.string.my_book)};

		public GoogleMusicAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int position) {
						
			
			switch (position) {
			case 0:
				resultPage = new ListViewBookStand(getActivity(),1);
				break;
			case 1:
				resultPage = new MyBookFragment();
				break;
			}
			return resultPage;
		}

		@Override
		public CharSequence getPageTitle(int position) {
			return CONTENT[position];
		}

		@Override
		public int getCount() {
			return CONTENT.length;
		}

	}
	public void ManageQuickAction(int type) {
		ActionItem bookName = new ActionItem(BOOK_NAME_ID,getResources().getString(R.string.BOOK_NAME));
		ActionItem bookDate = new ActionItem(BOOK_DATE_ID,getResources().getString(R.string.BOOK_DATE));
		ActionItem bookFav = new ActionItem(BOOK_FAV,getResources().getString(R.string.BOOK_FAVORITE));

		qa = new QuickAction(getActivity(), QuickAction.VERTICAL);
		qa.addActionItem(bookName);
		qa.addActionItem(bookDate);
		qa.addActionItem(bookFav);

		qa.setOnActionItemClickListener(new OnActionItemClickListener() {

			@Override
			public void onItemClick(QuickAction source, int pos, int actionId) {
				// TODO Auto-generated method stub
				ActionItem actionItem = qa.getActionItem(pos);
				if (actionId == BOOK_NAME_ID) {
					//sortMenu.setText(actionItem.getTitle().toString());
					//new LoadingListOfBook(activity,1).execute();
				} else if (actionId == BOOK_FAV) {
					//sortMenu.setText(actionItem.getTitle().toString());
					//new LoadingListOfBook(activity,1).execute();
				} else if (actionId == BOOK_DATE_ID) {
					//sortMenu.setText(actionItem.getTitle().toString());
					//new LoadingListOfBook(activity,1).execute();
				}
			}
		});
	}
	
}
